package com.example.videocompressors.slice;

import com.example.videocompressors.MediaController;
import com.example.videocompressors.ResourceTable;


import com.example.videocompressors.VideoSlimmer;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ProgressBar;
import ohos.agp.components.Text;

import ohos.app.Environment;
import ohos.data.resultset.ResultSet;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.audio.SoundPlayer;
import ohos.media.photokit.metadata.AVStorage;
import ohos.security.SystemPermission;


import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import static ohos.bundle.IBundleManager.PERMISSION_GRANTED;


public class MainAbilitySlice extends AbilitySlice {
    static HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00101, "MainAbilitySlice");
    private static final int REQUEST_FOR_VIDEO_FILE = 1000;
    private Text tv_input, tv_output, tv_indicator, tv_progress;
    private String outputDir = "/storage/emulated/0/Movies/";
    private String inputPath;
    private String outputPath;
    private ProgressBar pb_compress;
    private long startTime, endTime;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        requestCameraPermission();
        Button btn_select = (Button) findComponentById(ResourceTable.Id_btn_select);
        File sourcePath = getContext().getExternalFilesDir(Environment.DIRECTORY_MOVIES);
        File Path = new File(sourcePath, "aaa.mp4");
        btn_select.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //VideoSlimmer.convertVideo(inputPath, destPath, 200, 360, 200 * 360 * 30, new VideoSlimmer.ProgressListener()
                MediaController.getInstance(getContext()).convertVideo(sourcePath.getPath(),sourcePath,720,720,1);
//                VideoSlimmer.convertVideo(sourcePath.getPath() + "/bbb.mp4", Path.getPath(), 200, 360, 200 * 360 * 30, new VideoSlimmer.ProgressListener() {
//                    @Override
//                    public void onStart() {
//
//                    }
//
//                    @Override
//                    public void onFinish(boolean result) {
//
//                    }
//
//                    @Override
//                    public void onProgress(float progress) {
//
//                    }
//                });
            }
        });
    }

    private void requestCameraPermission() {
        List<String> permissions =
                new LinkedList<String>(
                        Arrays.asList(
                                SystemPermission.WRITE_USER_STORAGE,
                                SystemPermission.READ_USER_STORAGE,
                                SystemPermission.CAMERA,
                                SystemPermission.READ_MEDIA,
                                SystemPermission.WRITE_MEDIA));
        permissions.removeIf(
                permission ->
                        verifySelfPermission(permission) == PERMISSION_GRANTED || !canRequestPermission(permission));

        if (!permissions.isEmpty()) {
            requestPermissionsFromUser(permissions.toArray(new String[permissions.size()]), 1);
        } else {
            // PermissionBridge.getHandler().sendEvent(EVENT_PERMISSION_GRANTED);
        }
    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
